/**function showAllDocs(aPopup) {
  const nsILocalFile = Components.interfaces.nsILocalFile;
  var basePath = "/usr/share/gtk-doc/html";
  var folder =Components.classes["@mozilla.org/file/local;1"].createInstance(nsILocalFile);
  folder.initWithPath(basePath);

  var iter = folder.directoryEntries;
  while(iter.hasMoreElements()) {
    var file = iter.getNext().QueryInterface(nsILocalFile);
    if(file.isDirectory()) {
      var devhelpFile = file.path+"/"+file.leafName+".devhelp2";
      var realFile = Components.classes["@mozilla.org/file/local;1"].createInstance(nsILocalFile);
      realFile.initWithPath(devhelpFile);
      if(realFile.exists()) {
        var xmlDoc = document.implementation.createDocument("","",null);
        xmlDoc.async = "false";
        xmlDoc.load("file://"+realFile.path);
      }
    }
  }
}*/

function createListItem(aLabel, type, idx) {
  const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  var item = document.createElement("listitem");
	item.setAttribute("label", aLabel);
	return item;
}

function createMenuItem(aLabel, idx) {
  const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  var item = document.createElementNS(XUL_NS, "menuitem");
  item.setAttribute("label", aLabel);
	item.setAttribute("oncommand", "ghcore.onFilter("+idx+");");
  return item;
}

function ghInfo(menuitem, path) {
  this.item = menuitem;
  this.path = path;
}

function kwInfo(type, name, link) {
	this.type = type;
	this.name = name;
	this.link = link;
}

var ghcore = {
  ghinfo: new Array,
  kwinfo: new Array,
	xdinfo: new Array,
  initialize: false,
	pminit: false,

  init: function () {
    if(!this.initialize) {
      var title = this.xmlDoc.getElementsByTagName("book")[0].attributes.getNamedItem("title").nodeValue;
      var item = createMenuItem(title);
      this.ghinfo.push(new ghInfo(item, this.docs));
			var keyword = this.xmlDoc.getElementsByTagName("keyword");
			for(var idx=0; idx<keyword.length; idx++) {
				this.kwinfo.push(new kwInfo(keyword[idx].attributes[0].nodeValue, keyword[idx].attributes[1]. nodeValue,
							keyword[idx].attributes[2].nodeValue));
			}
      this.initialize = true;
    }
  },
  loadDoc: function(doc) {
    const myID = "gtkhelp@sourceforge.net";
    const nsILocalFile = Components.interfaces.nsILocalFile;

    //alert("start load");
    var em =Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager);
    this.path = em.getInstallLocation(myID).getItemLocation(myID).path;
    var realPath = this.path+"/chrome/content/"+doc+".devhelp2";
    var realFile = Components.classes["@mozilla.org/file/local;1"].createInstance(nsILocalFile);
    realFile.initWithPath(realPath);
		//alert(realPath);
    if(realFile.exists()) {
			//alert("load xmldoc");
      var xmlDoc = document.implementation.createDocument("","",null);
      xmlDoc.async = "false";
      xmlDoc.load("chrome://gtkhelp/content/"+doc+".devhelp2");
			this.xdinfo.push(xmlDoc);

      /**var len = this.xmlDoc.getElementsByTagName("keyword").length;
      alert("function number "+len);*/
    }
  },

  onFilter: function(idx) {
    var xmlDoc = this.xdinfo[idx];
    var keyword = xmlDoc.getElementsByTagName("keyword");
    var listbox = document.getElementById("searchList");
		//alert("total keyword item = " + keyword.length);
		for(var idx=0; idx<1000; idx++) {
			var item = createListItem(keyword[idx].attributes[1].nodeValue, keyword[idx].attributes[0].nodeValue, idx);
			listbox.appendChild(item);
		}
  },

  onLoad: function() {
    /*var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
    prefs = prefs.getBranch("extensions.gtkhelp.");
    var selected = spliteDocs(prefs.getCharPref("selected.docs");
    for(var i=0; i<selected.length; i++) {
		  loadDoc(selected[i]);
		}*/
    ghcore.loadDoc("gtk");
  },

  onShowAllDocs: function() {
	  if(!this.pminit) {
      var popup = document.getElementById("showAllDocsMenu");
      for(var i=0; i<this.xdinfo.length; i++) {
        var title = this.xdinfo[i].getElementsByTagName("book")[0].attributes.getNamedItem("title").nodeValue;
        var item = createMenuItem(title, i);
        popup.appendChild(item);
      }
			this.pminit = true;
		}
  },

  onSearch: function () {
	}
};

window.addEventListener("load", function(e) {ghcore.onLoad(e);}, false);
